package com.caifu.controller;

import com.caifu.bean.Result;
import com.caifu.bean.ResultUtil;
import com.caifu.service.ZcOrderingRecordDetailsService;
import com.caifu.util.Util;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.*;

/**
 * @Auther: Lyf
 * @Date: 2020/11/30 18:24
 * @Description:
 */
@Controller
@RequestMapping("/foodStatistics")
public class FoodStatisticsController {

    @Autowired
    private ZcOrderingRecordDetailsService orderingRecordDetailsService;

    @GetMapping("index")
    public String index(){
        return "foodDelivery/statistics/index";
    }

    @PostMapping("costStatistics")
    @ResponseBody
    public Result costStatistics(){
        try{
            Map<String,String> map = orderingRecordDetailsService.costStatistics();
            BigDecimal cost = new BigDecimal(String.valueOf(map.get("cost")));
            BigDecimal subsidy = new BigDecimal(String.valueOf(map.get("subsidy")));
            map.put("cost",cost.doubleValue()+"");
            map.put("subsidy",subsidy.doubleValue()+"");
            map.put("meals",cost.subtract(subsidy).doubleValue()+"");
            return new Result(200,"获取数据成功！",map);
        }catch (Exception e){
            e.printStackTrace();
            return ResultUtil.error(-1,"获取数据失败！");
        }
    }

    @PostMapping("properStatistics")
    @ResponseBody
    public Result properStatistics(){
        try{
            Map<String,String> orgMap = new LinkedHashMap<>();
            orgMap.put("1206","安亭片");
            orgMap.put("1207","黄渡片");
            orgMap.put("1208","方泰片");
            String month = Util.dateToString(null,"yyyyMM");
            List<Map<String,String>> result = new ArrayList<>();
            List<String> title = new ArrayList<>();
            int allCount = 0;
            for(Map.Entry<String, String> entry : orgMap.entrySet()){
                Map<String,String> m  = new HashMap<>();
                String mapKey = entry.getKey();
                String mapValue = entry.getValue();
                List<String> list = orderingRecordDetailsService.getListByParentId(mapKey);
                String count = orderingRecordDetailsService.properStatistics(list,month);
                m.put("name",mapValue);
                title.add(mapValue);
                m.put("value",count);
                allCount += Integer.parseInt(count);
                result.add(m);
            }
            JSONObject obj = new JSONObject();
            obj.put("data",result);
            obj.put("allCount",allCount);
            obj.put("title",title);
            return new Result(200,"获取数据成功！",obj);
        }catch (Exception e){
            e.printStackTrace();
            return ResultUtil.error(-1,"获取数据失败！");
        }
    }

    @PostMapping("ageStatistics")
    @ResponseBody
    public Result ageStatistics(){
        try{
            Map<String,String> ageMap = new LinkedHashMap<>();
            ageMap.put("60-69","60-69岁");
            ageMap.put("70-79","70-79岁");
            ageMap.put("80-89","80-89岁");
            ageMap.put("90-99","90-99岁");
            ageMap.put("100+","100岁及以上");
            List<Map<String,String>> list = orderingRecordDetailsService.ageStatistics();
            Map<String,String> map = new HashMap<>();
            int max = 0;
            if(list != null && list.size() > 0){
                for(Map<String,String> m : list){
                    map.put(m.get("age_str"),String.valueOf(m.get("count")));
                }
            }
            List<String> names = new ArrayList<>();
            List<String> values = new ArrayList<>();
            for(Map.Entry<String, String> entry : ageMap.entrySet()){
                String mapKey = entry.getKey();
                String mapValue = entry.getValue();
                names.add(mapValue);
                String count = map.get(mapKey);
                if(StringUtils.isNotBlank(count)){
                    values.add(count);
                    if(Integer.parseInt(count) > max){max = Integer.parseInt(count);}
                }else{
                    values.add("0");
                }
            }
            JSONObject obj = new JSONObject();
            obj.put("names",names);
            obj.put("values",values);
            obj.put("max",max);
            return new Result(200,"获取数据成功！",obj);
        }catch (Exception e){
            e.printStackTrace();
            return ResultUtil.error(-1,"获取数据失败！");
        }
    }

    @PostMapping("subsidyStatistics")
    @ResponseBody
    public Result subsidyStatistics(){
        try{
            Map<String,String> monthMap = new LinkedHashMap<>();
            String year = Util.dateToString(null,"yyyy");
            monthMap.put(year+"01",year+"-01");
            monthMap.put(year+"02",year+"-02");
            monthMap.put(year+"03",year+"-03");
            monthMap.put(year+"04",year+"-04");
            monthMap.put(year+"05",year+"-05");
            monthMap.put(year+"06",year+"-06");
            monthMap.put(year+"07",year+"-07");
            monthMap.put(year+"08",year+"-08");
            monthMap.put(year+"09",year+"-09");
            monthMap.put(year+"10",year+"-10");
            monthMap.put(year+"11",year+"-11");
            monthMap.put(year+"12",year+"-12");
            List<Map<String,String>> list1 = orderingRecordDetailsService.subsidyStatistics(orderingRecordDetailsService.getListByParentId("1206"));  //安亭片
            List<Map<String,String>> list2 = orderingRecordDetailsService.subsidyStatistics(orderingRecordDetailsService.getListByParentId("1207"));  //黄渡片
            List<Map<String,String>> list3 = orderingRecordDetailsService.subsidyStatistics(orderingRecordDetailsService.getListByParentId("1208"));  //方泰片
            Map<String,String> map1 = new HashMap<>();
            Map<String,String> map2 = new HashMap<>();
            Map<String,String> map3 = new HashMap<>();
            if(list1 != null && list1.size() > 0){
                for(Map<String,String> m : list1){
                    map1.put(m.get("month"),String.valueOf(m.get("subsidy")));
                }
            }
            if(list2 != null && list2.size() > 0){
                for(Map<String,String> m : list2){
                    map2.put(m.get("month"),String.valueOf(m.get("subsidy")));
                }
            }
            if(list3 != null && list3.size() > 0){
                for(Map<String,String> m : list3){
                    map3.put(m.get("month"),String.valueOf(m.get("subsidy")));
                }
            }
            List<String> names = new ArrayList<>();
            List<String> values1 = new ArrayList<>();
            List<String> values2 = new ArrayList<>();
            List<String> values3 = new ArrayList<>();
            Double max = 0.0;
            for(Map.Entry<String, String> entry : monthMap.entrySet()){
                String mapKey = entry.getKey();
                String mapValue = entry.getValue();
                Map<String,String> m  = new HashMap<>();
                names.add(mapValue);
                String count1 = map1.get(mapKey);
                if(StringUtils.isNotBlank(count1)){
                    if(Double.parseDouble(count1) > max){max = Double.parseDouble(count1);}
                    values1.add(count1);
                }else{
                    values1.add("0");
                }
                String count2 = map2.get(mapKey);
                if(StringUtils.isNotBlank(count2)){
                    if(Double.parseDouble(count2) > max){max = Double.parseDouble(count2);}
                    values2.add(count2);
                }else{
                    values2.add("0");
                }
                String count3 = map3.get(mapKey);
                if(StringUtils.isNotBlank(count3)){
                    if(Double.parseDouble(count3) > max){max = Double.parseDouble(count3);}
                    values3.add(count3);
                }else{
                    values3.add("0");
                }
            }
            JSONObject obj = new JSONObject();
            obj.put("names",names);
            obj.put("values1",values1);
            obj.put("values2",values2);
            obj.put("values3",values3);
            if((max+"").indexOf(".") != -1) {
                String temp = ((max + 1) + "");
                obj.put("max", Integer.parseInt(temp.substring(0, temp.indexOf("."))));
            }else{
                obj.put("max", max + "");
            }
            return new Result(200,"获取数据成功！",obj);
        }catch (Exception e){
            e.printStackTrace();
            return ResultUtil.error(-1,"获取数据失败！");
        }
    }

    @PostMapping("mealsCountStatistics")
    @ResponseBody
    public Result mealsCountStatistics(){
        try{
            Map<String,String> monthMap = new LinkedHashMap<>();
            String year = Util.dateToString(null,"yyyy");
            monthMap.put(year+"01",year+"-01");
            monthMap.put(year+"02",year+"-02");
            monthMap.put(year+"03",year+"-03");
            monthMap.put(year+"04",year+"-04");
            monthMap.put(year+"05",year+"-05");
            monthMap.put(year+"06",year+"-06");
            monthMap.put(year+"07",year+"-07");
            monthMap.put(year+"08",year+"-08");
            monthMap.put(year+"09",year+"-09");
            monthMap.put(year+"10",year+"-10");
            monthMap.put(year+"11",year+"-11");
            monthMap.put(year+"12",year+"-12");
            List<Map<String,String>> list1 = orderingRecordDetailsService.mealsCountStatistics(orderingRecordDetailsService.getListByParentId("1206"));  //安亭片
            List<Map<String,String>> list2 = orderingRecordDetailsService.mealsCountStatistics(orderingRecordDetailsService.getListByParentId("1207"));  //黄渡片
            List<Map<String,String>> list3 = orderingRecordDetailsService.mealsCountStatistics(orderingRecordDetailsService.getListByParentId("1208"));  //方泰片
            Map<String,String> map1 = new HashMap<>();
            Map<String,String> map2 = new HashMap<>();
            Map<String,String> map3 = new HashMap<>();
            if(list1 != null && list1.size() > 0){
                for(Map<String,String> m : list1){
                    map1.put(m.get("month"),String.valueOf(m.get("count")));
                }
            }
            if(list2 != null && list2.size() > 0){
                for(Map<String,String> m : list2){
                    map2.put(m.get("month"),String.valueOf(m.get("count")));
                }
            }
            if(list3 != null && list3.size() > 0){
                for(Map<String,String> m : list3){
                    map3.put(m.get("month"),String.valueOf(m.get("count")));
                }
            }
            List<String> names = new ArrayList<>();
            List<String> values1 = new ArrayList<>();
            List<String> values2 = new ArrayList<>();
            List<String> values3 = new ArrayList<>();
            int max = 0;
            for(Map.Entry<String, String> entry : monthMap.entrySet()){
                String mapKey = entry.getKey();
                String mapValue = entry.getValue();
                Map<String,String> m  = new HashMap<>();
                names.add(mapValue);
                String count1 = map1.get(mapKey);
                if(StringUtils.isNotBlank(count1)){
                    if(Integer.parseInt(count1) > max){max = Integer.parseInt(count1);}
                    values1.add(count1);
                }else{
                    values1.add("0");
                }
                String count2 = map2.get(mapKey);
                if(StringUtils.isNotBlank(count2)){
                    if(Integer.parseInt(count2) > max){max = Integer.parseInt(count2);}
                    values2.add(count2);
                }else{
                    values2.add("0");
                }
                String count3 = map3.get(mapKey);
                if(StringUtils.isNotBlank(count3)){
                    if(Integer.parseInt(count3) > max){max = Integer.parseInt(count3);}
                    values3.add(count3);
                }else{
                    values3.add("0");
                }
            }
            JSONObject obj = new JSONObject();
            obj.put("names",names);
            obj.put("values1",values1);
            obj.put("values2",values2);
            obj.put("values3",values3);
            obj.put("max", max );
            return new Result(200,"获取数据成功！",obj);
        }catch (Exception e){
            e.printStackTrace();
            return ResultUtil.error(-1,"获取数据失败！");
        }
    }
}
